<?php
namespace Home\Controller;
use Think\Controller;
class ArticleController extends CommntController {
    public function index()
    {
        $moder   = D('Article');
        $num     = 5;
        $_tmp    = clone $moder;
        $count   = $_tmp->count();
        $Page    = new \Think\Page($count,$num);
        $article = $_tmp->order('article.hits desc')->limit($Page->firstRow.','.$Page->listRows)->where(array('status'=>0))->select();
        $show  = $Page->show();// 分页显示输出
        $this->assign('page',$show);
        $this->assign('article',$article);
        $this->display();
    }
    /**
     * 文章发布
     */
    public function article_add()
    {
        $category = category_ppd();
        $this->assign('moder',$category);
        $this->display();
    }
    /**
     * 文章发布
     */
    public function add()
    {
        //判断是否是POST传过来的值
        if (!IS_POST) {
            echo "1";die;
        }

        $file = $_FILES['imgs'];
        $data = $_POST;
        $filetmen = substr(strrchr($file['name'],'.'),1);
        $timr = time().rand(1,10);
        $imag = $timr.'.'.$filetmen;
        //判断图片类型是否正确
        if (isset($file) && $file['name'] != '') {
            $arr = array("image/jpg","image/png","image/jpeg","image/gif");
            $fiel_type = $file['type'];
            if (!in_array($fiel_type,$arr)) {
                echo "2";die;//对不起您上传的不是图片
            }
        }
        $path = './public/article/'.$imag;
        if (is_uploaded_file($file['tmp_name'])) {
            if (move_uploaded_file($file['tmp_name'],$path)) {
                $image = new \Think\Image();//图片处理类
                $image->open($path);
                $image->thumb(229, 195,\Think\Image::IMAGE_THUMB_SCALE)->save($path);
                clearstatcache();//清除缓存
                if (filesize($path)>(50*1024)) {
                     $image->thumb(400,400,\Think\Image::IMAGE_THUMB_SCALE)->save($path);
                }
                $data['img'] = $imag;
            }
        }
        $data['add_time'] = date('Y-m-d H:i:s',time());
        $data['update_time'] = date('Y-m-d H:i:s',time());
        $article      = M('article');
        if ($article->add($data)) {
            //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
            // $this->success('添加成功', U('Article/index'));
            $url = U('Index/index');
            echo "3#".$url;die;
        }else{
            $url = U('Article/article_add');
            echo "4#".$url;die;
        }
    }
    /**
     * 文章
     */
    public function article_list($id = '')
    {   
        $pid = 0;
        $user_id = session('user_id');
        $cid=intval(cookie('cid'));
        // cookie('cid',$user_id,2);
        // $this->pp($cid);die();
        $id = $_GET['id'];
        // 判断是否已经记录过aid
        if (array_key_exists($cid, $read)) {
            // 判断点击本篇文章的时间是否已经超过一天
            if ($read[$id]-time()>=86400) {
                $read[$id]=time();
                // 文章点击量+1
                M('Article')->where(array('id'=>$id))->setInc('click',1);
            }
        }else{
            $read[$id]=time();
            // 文章点击量+1
            M('Article')->where(array('id'=>$id))->setInc('click',1);
        }
        cookie('cid',$id,864000);
        $article = D('Article')->where(array('id'=>$id))->find();
        if( $article ){
            foreach ($article as $key => $value) {
               $article['name'] =  D('User')->where(array('user_id'=>$value['id']))->find()['name'];
            }
        }
        
        // var_dump($article);die;
        $this->assign('article',$article);
        $this->display();        
    }


    public function article_category($id = '')
    {   
        $id = $_GET['id'];
        $moder = D('Article');
        $articles=$moder->getPageData($id);
        // $this->pp($articles);die();
        // $id = $_GET['id'];
        // $model = D('Article')->where(array('category_id'=>$id))->select();
        $category = D('Category')->where(array('id'=>$id))->field('name,count')->find();
        // $this->pp($category);die(); 
        $artile = $moder->where(array('iselite'=>1))->field('id,title')->select();
        $this->assign('article',$articles);
        // $this->pp($articles);die();
        $this->assign('articletile',$artile);
        $this->assign('category',$category);
        $this->display();
    }
}